using System;
using System.IO;
using System.Text;

namespace ICommonLibrary.Resource {

    public class StringResource : IUpdatableResource{

        public string Description { get; private set; }
        public string Text { get; private set; }

        public string Author { get; set; }
        public DateTime? LastModified { get; private set; }

        public StringResource(string text) { 
            Asserts.NotNull(text, "Text resource is null");

            Text = text;
            Description = string.Format("StringResource[Length={0}]", text.Length);
            LastModified = DateTime.UtcNow;
        }

        public Stream GetStream() {
            return new MemoryStream(Encoding.UTF8.GetBytes(Text), false);
        }

        public TextReader GetReader() {
            return new StringReader(Text);
        }

        public void Update(Stream input) {
            Text = IOUtil.ReadStringAndClose(new StreamReader(input));
            LastModified = DateTime.UtcNow;
        }

        public void Update(TextReader input) {
            Text = IOUtil.ReadStringAndClose(input);
            LastModified = DateTime.UtcNow;
        }
    }
}